package mars.activity;

import java.io.StringReader;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import bear.download.HttpsUtils;
import bear.download.MD5;
import bear.download.MyContentHandler;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class landActivity extends Activity{
	private Button determine,cancel;
	public static EditText user,password;
	private boolean key=true;
	public static String hash;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.land);
		user=(EditText)findViewById(R.id.user);
		password=(EditText)findViewById(R.id.password);        
        determine=(Button)findViewById(R.id.determine);
        cancel=(Button)findViewById(R.id.cancel);
        
        	 SharedPreferences preference = getSharedPreferences("person",Context.MODE_PRIVATE);
		        user.setText(preference.getString("User",""));            //preference.getString(标示符,默认值<这里为空>）
		        password.setText(preference.getString("Psw", ""));
		        
        cancel.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v) {
				// TODO Auto-generated method stub
				System.exit(0);
			}        	
        });
        determine.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				String name=user.getText().toString();
				String pass=password.getText().toString();
				//String name="s091669";
				//String pass="s091669";
				if(name.equals("")||pass.equals("")){
					Toast.makeText(landActivity.this,R.string.kong,Toast.LENGTH_SHORT).show();				
				}else{
					String up = name+pass;
					hash = MD5.md5(up);
				HttpsUtils hu=new HttpsUtils();			
				String validateUrl=hu.get("https://www.loushiapp.com/?c=login&a=userlogin&username="+name+"&password="+pass);
				try{
					SAXParserFactory spf = SAXParserFactory.newInstance();
					XMLReader reader=spf.newSAXParser().getXMLReader();
					reader.setContentHandler(new MyContentHandler());
					reader.parse(new InputSource(new StringReader(validateUrl)));
				}catch(Exception e){
					e.printStackTrace();
				}
				key=MyContentHandler.cuole;
				if(key==false){		
				Toast.makeText(landActivity.this,R.string.key,Toast.LENGTH_SHORT).show();
				}else {
					 SharedPreferences preference = getSharedPreferences("person",Context.MODE_PRIVATE);
				        Editor edit = preference.edit();
				        edit.putString("User",name);      
				        edit.putString("Psw",pass);
				        edit.commit();
					Intent intent = new Intent();
	                intent.setClass(landActivity.this, Qidong.class);
	                landActivity.this.startActivity(intent);
	                finish();
				}
			}
			}
        }
	);
	}
}
     		
